#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <vector>
#include <string>
#include <cstring>

//
//
//
/*
void* threadrun(void *args)
{
    int cnt = 2;
    while(cnt)
    {
        std::cout << "new thread is runing" <<  cnt-- << std::endl;
        sleep(1);
    }
    int *a = nullptr;
    *a = 10;
    return nullptr;
}
int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,threadrun,nullptr);

    sleep(2);
    // pthread_cancle(tid);关闭线程

    int cnt = 5;
    while(true)
    {
        std::cout << "main thread is runing" <<  cnt-- << std::endl;
        sleep(1);
    }

    return 0;
}
*/


/* 
// 全局变量，所有线程共享
int global_variable = 0;
// 线程函数，演示私有栈和共享全局变量
void* thread_function(void* arg)   
{    
    // 转换参数为int  
    int id = *reinterpret_cast<int*>(arg);  
    // 局部变量，每个线程私有  
    int local_variable = id;  
    // 打印局部变量的地址，用来表示栈的近似位置  
    std::cout << "Thread " << id << ": Local variable address = " << &local_variable << std::endl;  
    // 访问并修改共享的全局变量（无锁保护）  
    global_variable += local_variable;  
    // 打印修改后的全局变量值（注意，这里可能会与其他线程的输出交错，并且值可能是错误的）  
    std::cout << "Thread " << id << ": Modified global variable = " << global_variable << std::endl;  
    return NULL;  
}  
int main()   
{    
    std::vector<pthread_t> threads;  
    int thread_args[5] = {0, 1, 2, 3, 4};  
    // 创建多个线程，并保留线程ID  
    for (int i = 0; i < 5; ++i) {  
        threads.emplace_back(); // 为下一个线程ID预留空间  
        pthread_create(&threads[i], NULL, thread_function, reinterpret_cast<void*>(&thread_args[i]));  
    }  
    // 等待所有线程完成  
    for (size_t i = 0; i < threads.size(); ++i) {  
        pthread_join(threads[i], NULL);  
    }  
    // 打印最终的全局变量值  
    std::cout << "Final global variable = " << global_variable << std::endl;  
    return 0;    
}
*/

/*
const int threadnum = 5;
void *handlertask(void *args)
{
    std::string threadname = static_cast<char *>(args);
    while (true)
    {
        sleep(1);
        std::cout << "Iam" << threadname << std::endl;
    }
}
// 多线程创建
int main()
{
    std::vector<pthread_t> threads;
    for (int i = 0; i < threadnum; i++)
    {
        char threadname[64]; 
        snprintf(threadname, sizeof(threadname), "Thread-%d", i + 1); // 线程不安全
        pthread_t tid;
        pthread_create(&tid, nullptr, handlertask, threadname);
    }
    return 0;
}
*/

/*
void *thread_run(void *arg)
{
    pthread_detach(pthread_self());
    std::cout << (const char*)arg << std::endl;
    return nullptr;
}

int main() {
    pthread_t tid;
    if (pthread_create(&tid, nullptr, thread_run, (void*)"thread1 run...") != 0)
    {
        std::cout << "create thread error" << std::endl;
        return 1;
    }

    int ret = 0;
    sleep(1); // 很重要，要让线程先分离，再等待
    if (pthread_join(tid, NULL) == 0)
    {
        std::cout << "pthread wait success\n" << std::endl;
        ret = 0;
    }
    else
    {
        std::cout << "pthread wait failed\n" << std::endl;
        ret = 1;
    }
    return ret;

    return 0;
}
*/

/*
void *newthreadrun(void *args)
{
    int cnt = 3;
    while (cnt--)
    {
        std::cout << "I am new thread,pthread_t : " << pthread_self() << std::endl;
        sleep(1);
    }
    pthread_exit(nullptr);
}
int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, newthreadrun, nullptr);

    void *exitCode;
    pthread_join(tid,&exitCode);
    std::cout << "new pthread exitCode: " << std::endl;

    return 0;
}
*/

/*
void *newthreadrun(void *args)
{
    while (true)
    {
        std::cout << "I am new thread,pthread_t : " << pthread_self() << std::endl;
        sleep(1);
    }
}
int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, newthreadrun, nullptr);

    int cnt = 5, exitCode;
    while (cnt--)
    {
        std::cout << "I am main thread,pthread_t : " << pthread_self() << std::endl;
        if (cnt == 3)
        {
            exitCode = pthread_cancel(tid);
            std::cout << "new thread,exitCode : " << exitCode << std::endl;
        }
        sleep(1);
    }

    return 0;
}
*/

/* // pthread_exit
void *newthreadrun(void *args)
{
    pthread_exit((int*)args);
}
int main()
{
    pthread_t tid;
    int x = 10;
    pthread_create(&tid, nullptr, newthreadrun, (void*)&x);

    std::cout << "I new thread,exitCode : " << x << std::endl;

    return 0;
}
*/